<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
// 实现一个n维数组，填满0
// function matrix (n) {
//     let deep = n
//     let matrixArray = []
//     function deepMatrix (rootArr, deep) {
//         for (var i = 0; i < n; i++) {
//             let arr
//             if (deep <= 2) {
//                 arr = new Array(n)
//                 arr.fill(0)
//             } else {
//                 arr = new Array()
//                 deepMatrix(arr, deep-1)
//             }
//             rootArr.push(arr)
//         }
//     }
//     deepMatrix(matrixArray, deep)
//     return matrixArray
// }

// console.log(matrix(2))



// let arr = [
//     {
//         id: 100,
//         name: '安徽',
//         parent: 0
//     },
//     {
//         id: 1001,
//         name: '六安',
//         parent: 100
//     },
//     {
//         id: 1002,
//         name: '合肥',
//         parent: 100
//     },
//     {
//         id: 10001,
//         name: 'xx区',
//         parent: 1001
//     }
// ]





function ListNode(val, next) {
    this.val = (val===undefined ? 0 : val)
    this.next = (next===undefined ? null : next)
}

function createNode (arr) {
    let nodeArr = []
    for (let i = 0, length = arr.length; i < length; i++) {
        nodeArr.push(new ListNode(arr[i]))
    }
    console.log('nodeArr111111:', nodeArr)
    for (let i = 0, length = nodeArr.length; i < length; i++) {
        nodeArr[i].next = nodeArr[i + 1]
    }
    return nodeArr[0]
}

let arr = [1,2,3,4,5]

nodeArr = createNode(arr)

console.log('nodeArr:', nodeArr)



var removeNthFromEnd = function(head, n) {
    var target = 1 // length
    var point = head
    while (point.next) {
        target++
        point = point.next
    }
    console.log('length:', target)
    target = target - n // target
    point = head
    var pointNext = head
    console.log('target:', target)
    while (target) {
        point = pointNext
        console.log('ppppp:', point.val)
        pointNext = pointNext.next
        target--
        console.log('target:', target)
    }
    console.log('point:', point)
    console.log('pointNext:', pointNext)
    if (pointNext === head) {
        head = pointNext.next
    } else {
        point.next = pointNext.next
    }
    return head
}


console.log('remove node')

removeNthFromEnd(nodeArr, 1)

console.log()

</script>
</body>
</html>